package com.yanq.liancar.fragment;
/*
 *  @创建者     Administrator
 *  @创建时间   2018/10/23  16:43
 *  @描述       ${TODO}
 */


import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.gson.Gson;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import com.vondear.rxtool.view.RxToast;
import com.yanq.liancar.R;
import com.yanq.liancar.adapter.CouponAdapter;
import com.yanq.liancar.adapter.IntegralDetailAdapter;
import com.yanq.liancar.adapter.IntegralDetailAdapter1;
import com.yanq.liancar.adapter.ServiceQAdapter;
import com.yanq.liancar.base.BaseLYFragment;
import com.yanq.liancar.bean.ConsumptionIntegralBean;
import com.yanq.liancar.bean.CouponBean;
import com.yanq.liancar.bean.DaijinQuanBean;
import com.yanq.liancar.bean.IntegralDetailBean;
import com.yanq.liancar.bean.UserBean;
import com.yanq.liancar.url.Config;
import com.yanq.liancar.util.ToastUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

public class IntegralDetailFragment extends BaseLYFragment {

    @BindView(R.id.recyclerview)
    RecyclerView recyclerview;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    @BindView(R.id.ll_noMessage)
    LinearLayout llNoMessage;
    Unbinder unbinder;
    private int page = 1;
    private boolean isClear = true;
    private boolean isLastPage;
    private String url;
    private int type;
    private IntegralDetailAdapter mAdapter;
    private IntegralDetailAdapter1 mAdapter1;
    private List<IntegralDetailBean.DataBean.ListBean> mFList = new ArrayList<>();
    private List<ConsumptionIntegralBean.DataBean.ListBean> mList = new ArrayList<>();

    public static IntegralDetailFragment newInstance(int type) {
        Bundle bundle = new Bundle();
        bundle.putInt("type", type);
        IntegralDetailFragment integralDetailFragment = new IntegralDetailFragment();
        integralDetailFragment.setArguments(bundle);
        return integralDetailFragment;
    }

    @Override
    protected void onCreateViewLazy(Bundle savedInstanceState) {
        super.onCreateViewLazy(savedInstanceState);
        setContentView(R.layout.fragment_integraldetail);
        type = getArguments().getInt("type");

        initRecyclerview();
        initListener();

        if (type == 0) {
            url = "llc/IntegralDetailController/integralDetail";
        } else {
            url = "llc/api/ConsumpeIntegralRecordController/selectConsumpeIntegralRecord";
        }

        initOkGoWhole();
    }

    private void initRecyclerview() {
        recyclerview.setNestedScrollingEnabled(false);
        recyclerview.setLayoutManager(new LinearLayoutManager(mActivity));
    }

    private void initListener() {
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                page = 1;
                isClear = true;

                initOkGoWhole();
                refreshlayout.finishRefresh();
            }
        });
        refreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                page++;
                isClear = false;
                if (!isLastPage){
                    initOkGoWhole();
                }else {
                    ToastUtil.show(mActivity, "暂无更多数据");
                }
                refreshLayout.finishLoadMore();
            }

        });
    }


    //全部
    private void initOkGoWhole() {
        OkGo.<String>post(Config.s + url)
                .tag(this)
                .params("token", UserBean.getcToken(mActivity))
                .params("pageNum", page)
                .params("pageSize", 10)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        Gson gson = new Gson();
                        if (type == 0) {
                            IntegralDetailBean bean = gson.fromJson(response.body(), IntegralDetailBean.class);
                            if (bean.isIsSuccess()) {
                                if (bean.getCode()==200){
                                    isLastPage = bean.getData().isIsLastPage();
                                    if (isClear) {
                                        mFList.clear();
                                    }
                                    mFList.addAll(bean.getData().getList());
                                    mAdapter = new IntegralDetailAdapter(R.layout.item_integraldetail, mFList);
                                    recyclerview.setAdapter(mAdapter);
                                }else {
                                    RxToast.normal(bean.getOutputMessage());
                                }

                                //                                if (mDList.size() < 1) {
                                //                                    ll_noMessage.setVisibility(View.VISIBLE);
                                //                                } else {
                                //                                    ll_noMessage.setVisibility(View.GONE);
                                //                                }
                            } else {
                                RxToast.error(bean.getOutputMessage());
                            }
                        } else {
                            ConsumptionIntegralBean bean = gson.fromJson(response.body(), ConsumptionIntegralBean.class);
                            if (bean.isIsSuccess()) {
                                if (bean.getCode()==200){
                                    isLastPage = bean.getData().isIsLastPage();
                                    if (isClear) {
                                        mList.clear();
                                    }
                                    mList.addAll(bean.getData().getList());
                                    mAdapter1 = new IntegralDetailAdapter1(R.layout.item_integraldetail, mList);
                                    recyclerview.setAdapter(mAdapter1);
                                }else {
                                    RxToast.normal(bean.getOutputMessage());
                                }
                                //                                if (mFList.size() < 1) {
                                //                                    ll_noMessage.setVisibility(View.VISIBLE);
                                //                                } else {
                                //                                    ll_noMessage.setVisibility(View.GONE);
                                //                                }

                            } else {
                                RxToast.error(bean.getOutputMessage());
                            }
                        }

                    }

                    @Override
                    public void onError(Response<String> response) {
                        RxToast.error("服务器连接超时");
                        super.onError(response);
                    }
                });

    }
}
